package br.fatec.servico;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;

import br.fatec.dominio.responsavel.Responsavel;

public class MySQLEstagioDAO implements IEstagioDAO {
	private static final String consulta = "SELECT * FROM estagio WHERE ra LIKE ? ";
	private static final String altera = "UPDATE estagio SET nome = ?, curso = ?,  dataInicio = ?,  dataFim = ? , cnpjEmpresa = ?, codResponsavel = ? WHERE ra like ? ";
	private static final String exclui = "DELETE FROM estagio WHERE ra like ? ";
	private static final String cadastra = "INSERT INTO estagio (ra, nome, curso, dataInicio, dataFim, cnpjEmpresa, codResponsavel) VALUES (?, ?, ?, ?, ?, ?, ?) ";
	String msg = "";

	@Override
	public String getMessage() {
		// TODO Auto-generated method stub
		return msg;
	}

	@Override
	public ResultSet consulta(String ra) {
		ResultSet rs = null;
		PreparedStatement stmt;
		try {
			stmt = MySQLDAOFactory.criaConexao().prepareStatement(consulta);
			stmt.setString(1, ra);
			rs = stmt.executeQuery();			
			// stmt.close();
		} catch (SQLException e) {
			msg = e.getMessage();
		}
		return rs;
	}

	@Override
	public int altera(String ra, String nome, Date dataInicio, Date dataFim,
			String empresa, int codResponsavel, String curso) {
		PreparedStatement stmt;
		int rc = 0;
		try {
			stmt = MySQLDAOFactory.criaConexao().prepareStatement(altera);
			stmt.setString(1, nome);
			stmt.setString(2, curso);
			if (dataInicio !=  null){
				stmt.setDate(3, new java.sql.Date(dataInicio.getTime()));	
			}if (dataFim !=  null){
				stmt.setDate(4, new java.sql.Date(dataFim.getTime()));
			}	
			stmt.setString(5, empresa);
			stmt.setInt(6, codResponsavel);
			stmt.setString(7, ra);
			rc = stmt.executeUpdate();			
			stmt.close();		
		} catch (SQLException e) {
			System.out.print("veio pro catch");
			msg = e.getMessage();	
			System.out.print(msg);
		}
		return rc;
	}

	@Override
	public int exclui(String ra) {
		PreparedStatement stmt;
		int rc = 0;
		try {
			stmt = MySQLDAOFactory.criaConexao().prepareStatement(exclui);
			stmt.setString(1, ra);
			rc = stmt.executeUpdate();
			stmt.close();
		} catch (SQLException e) {
			msg = e.getMessage();
		}
		return rc;
	}

	@Override
	public int cadastra(String ra, String nome, String curso, Date dataInicio,
			Date dataFim, String cnpjEmpresa, int codResponsavel) {
		PreparedStatement stmt;
		int rc = 0;
		try {			
			stmt = MySQLDAOFactory.criaConexao().prepareStatement(cadastra);
			stmt.setString(1, ra);
			stmt.setString(2, nome);
			stmt.setString(3, curso);
			if (dataInicio !=  null){
				stmt.setDate(4, new java.sql.Date(dataInicio.getTime()));	
			}if (dataFim !=  null){
				stmt.setDate(5, new java.sql.Date(dataFim.getTime()));
			}	
			stmt.setString(6, cnpjEmpresa);
			stmt.setInt(7, codResponsavel);
			rc = stmt.executeUpdate();			
			stmt.close();		
		} catch (SQLException e) {
			msg = e.getMessage();	
		}
		return rc;	
	}
}
